//
//  ChangePasswordViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/8/8.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()

/**旧密码*/
@property (weak, nonatomic) IBOutlet UITextField *originalPwd;
/**新密码*/
@property (weak, nonatomic) IBOutlet UITextField *NewPwd;
/**新密码确认*/
@property (weak, nonatomic) IBOutlet UITextField *NewPwdConfirm;
/**手机验证*/
@property (weak, nonatomic) IBOutlet UIImageView *phoneProvingImg;
/**邮箱验证*/
@property (weak, nonatomic) IBOutlet UIImageView *emailProvingImg;

@end

@implementation ChangePasswordViewController
{
    NSString *_proving;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航栏
    [self setNav];
    
}
//设置导航栏
- (void)setNav{
    self.title = @"修改密码";
}

//手机验证
- (IBAction)phoneProving:(id)sender {
    _proving = @"0";
    _phoneProvingImg.image = [UIImage imageNamed:@"GLDZicon_08"];
    _emailProvingImg.image = [UIImage imageNamed:@"GLDZicon_13"];
}
//邮箱验证
- (IBAction)emailProving:(id)sender {
    _proving = @"1";
    _phoneProvingImg.image = [UIImage imageNamed:@"GLDZicon_13"];
    _emailProvingImg.image = [UIImage imageNamed:@"GLDZicon_08"];
}
//发送验证码
- (IBAction)sendVerification:(id)sender {
    NSLog(@"发送验证码");
}
//确认
- (IBAction)confirm:(id)sender {
    if ([_originalPwd.text isEqualToString:_NewPwd.text]) {
        [EBUtility prompt:@"新密码与旧密码一致" with:self];
    }else if (![_NewPwd.text isEqualToString:_NewPwdConfirm.text]) {
        [EBUtility prompt:@"两次新密码不一致" with:self];
    }else [self confirmOriginalPwd];
}

- (void)confirmOriginalPwd{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *para = @{
                           @"memId"     : @"0",
                           @"loginname" : [defaults objectForKey:@"UserName"],
                           @"password"  : [_originalPwd.text MD5Hash],
                           @"Uid"       : @""
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypeGet withMethodName:@"7494/api/Member/GetMember?" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        if ([object[@"Code"] isEqualToString:@"0"]) {
            [self changePassword];
        }else{
            [EBUtility prompt:@"旧密码输入有误" with:self];
        }
        
    } withFailureBlock:^(NSError *error) {
        [EBUtility prompt:@"网络请求失败" with:self];
    } progress:nil];

}

#pragma mark === 会员修改密码 ===
- (void)changePassword{
    NSUserDefaults *defaluts = [NSUserDefaults standardUserDefaults];
    if (![defaluts objectForKey:@"Id"]) {
        [EBUtility prompt:@"请登录" with:self];
        return;
    }
    
    NSDictionary *para = @{
                           @"UserPassword" : _NewPwd.text,
                           @"RowStatus"    : @"UPDATE",
                           @"Id"           : [defaluts objectForKey:@"Id"]
                           };
    
    NSString *paraString = [NetWorkManager dictionaryToJson:para];
    
    NSDictionary *paraments = @{
                                @"tableId" : @"294",
                                @"command" : @"onModifyMemberPassword",
                                @"params"  : paraString,
                                @"Id"      : @""
                                };
    [NetWorkManager requestWithMethodName:@"12082/api/Rest/RestProcess?" withParaments:paraments withSuccessBlock:^(NSDictionary *object) {
        [EBUtility prompt:object[@"Message"] with:self];
        [self.navigationController popViewControllerAnimated:YES];
    } withFailureBlock:^(NSError *error) {
        [EBUtility prompt:@"网络请求失败" with:self];
    } progress:nil];
    
}


@end
